package com.dianrui.qiyouriding.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.event.RfreshUnread;
import com.dianrui.qiyouriding.util.MyUtil;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的消息
 */
public class MessageDeatilsActivity extends BaseActivity {
    private String mMessageId;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.art_title)
    TextView artTitle;
    @BindView(R.id.art_time)
    TextView artTime;
    @BindView(R.id.art_content)
    TextView artContent;


    @Override
    public int getContentViewId() {
        return R.layout.activity_messagedetails;
    }

    @Override
    public void initView() {
        customInit(true,R.color.main_color);
        title.setText(getString(R.string.msg_details));
        if (getIntent() != null && getIntent().getExtras()!=null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                mMessageId=bundle.getString("id");
            }
        }
        getMsgDetails();
    }

    @Override
    public void initData() {}

    @Override
    protected void onStop() {//程序切后台
        super.onStop();
        if (MyUtil.isAppIsInBackground(MessageDeatilsActivity.this)) {
            EventBus.getDefault().post(new RfreshUnread());
        }
    }

    private void getMsgDetails() {
        JsonObject jsonObject=new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        jsonObject.addProperty("messages_id",mMessageId);
        OkHttpRequest.getInstance().postRequests(Url.MESSAGEINFO, jsonObject.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                artTitle.setText(jsonObject.optJSONObject("data").optString("title"));
                                artTime.setText(jsonObject.optJSONObject("data").optString("create_time"));
                                artContent.setText(jsonObject.optJSONObject("data").optString("contents"));
                            }else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }




    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
